package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherMessageAppMapper;
import com.zjdiepu.www.ibiz.other.IOtherMessageAppService;
import com.zjdiepu.www.model.other.dto.OtherMessageApp;
import com.zjdiepu.www.model.other.ext.OtherMessageAppExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年06月01日 18时07分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherMessageAppServiceImpl extends BaseService implements IOtherMessageAppService {
	public static final Logger logger = LoggerFactory.getLogger(OtherMessageAppServiceImpl.class);

	@Autowired
	private OtherMessageAppMapper otherMessageAppMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherMessageApp otherMessageApp, OperateType operType) {
		switch (operType) {
		case add:
			otherMessageApp.initInsertData(otherMessageApp.getModifier(), otherMessageApp.getModifierName());
			otherMessageAppMapper.insert(otherMessageApp);
			break;
		case edit:
			otherMessageApp.initUpdateData(otherMessageApp.getModifier(), otherMessageApp.getModifierName());
			// 影响的记录数
			int updateCount = otherMessageAppMapper.updateByPrimaryKeySelective(otherMessageApp);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherMessageApp.initDeleteData(otherMessageApp.getModifier(), otherMessageApp.getModifierName());
			// 影响的记录数
			int deleteCount = otherMessageAppMapper.updateByPrimaryKeySelective(otherMessageApp);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherMessageApp> selectByExt(OtherMessageAppExt otherMessageAppExt) {
		return otherMessageAppMapper.selectByExt(otherMessageAppExt);
	}
	
	@Override
	public int countByExt(OtherMessageAppExt otherMessageAppExt){
		return otherMessageAppMapper.countByExt(otherMessageAppExt);
	}
	
	@Override
	public OtherMessageApp selectById(String id) {
		return otherMessageAppMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherMessageAppMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherMessageApp otherMessageApp) {
		return otherMessageAppMapper.countRefined(otherMessageApp);
	}

	@Override
	public OtherMessageApp selectRefined(OtherMessageApp otherMessageApp) {
		return otherMessageAppMapper.selectRefined(otherMessageApp);
	}
	
	@Override
	public int deletedRefined(OtherMessageApp otherMessageApp) {
		return otherMessageAppMapper.deletedRefined(otherMessageApp);
	}

	@Override
	public boolean repeat(OtherMessageApp otherMessageApp) {
		int count = countRefined(otherMessageApp);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherMessageApp.getId())) {
			return true;
		}
		OtherMessageApp dbItem = selectRefined(otherMessageApp);
		if(null == dbItem) {
			return false;
		}
		return !otherMessageApp.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherMessageAppMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherMessageApp> otherMessageApps) {
		return otherMessageAppMapper.insertBatch(otherMessageApps);
	}
}
